package com.hemh.utils;

import java.io.FileInputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.hemh.entity.OmsBook;

/**
 * 
 * @author HeMingHui
 * @since 2020-10-29
 * @description 导出工具类
 */
public class ExportUtils {

	public static HSSFWorkbook exportBookList(String filePath, List<OmsBook> bookList) {
		HSSFWorkbook wb = null;/** 导出2003Excel */

		try {
			/** 读取excel模板*/
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));
			/** 读取excel模板 */
			wb = new HSSFWorkbook(fs);			
			/** 读取了模板内所有sheet内容 */
			HSSFSheet sheet = wb.getSheetAt(0);
			/** 在相应的单元格进行赋值 */
			int rowIndex = 1;/** 不包含表头 */
			for (OmsBook item : bookList) {
				HSSFRow row = sheet.getRow(rowIndex);
				if (null == row) {
					row = sheet.createRow(rowIndex);
				}
				HSSFCell cell0 = row.getCell(0);
				if (null == cell0) {
					cell0 = row.createCell(0);
				}
				cell0.setCellValue(item.getBookName());/** 书名 */
				HSSFCell cell1 = row.getCell(1);
				if (null == cell1) {
					cell1 = row.createCell(1);
				}
				cell1.setCellValue(item.getBookAuthorName());/** 图书作者 */
				HSSFCell cell2 = row.getCell(2);
				if (null == cell2) {
					cell2 = row.createCell(2);
				}
				cell2.setCellValue(item.getBookPress());/** 出版社 */
				HSSFCell cell3 = row.getCell(3);
				if (null == cell3) {
					cell3 = row.createCell(3);
				}
				cell3.setCellValue(item.getBookPrice().toString());/** 价格(数据库内存着float类型) */

				rowIndex++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return wb;
	}

}
